import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// /* webpackChunkName: 'Login_Home_Index' */是路由懒加载
const routes = [
 {path:'/',redirect:'/login'},
 {
   path:'/login',
   name:'login',
   component: () => import(/* webpackChunkName: 'Login_Home_Index' */ '@/components/Login.vue')
 },
 {
  path:'/home',
  name:'home',
  component: () => import(/* webpackChunkName: 'Login_Home_Index' */ '@/components/Home.vue'),
  redirect:'/index',  //访问hone就重定向到index
  children:[  //子路由
    {path:'/index', name:'index',component: () => import(/* webpackChunkName: 'Login_Home_Index' */ '@/components/Index.vue')},
    
    {path:'/users', name:'users',component: () => import(/* webpackChunkName: 'User_Role_Right' */ '@/components/user/Users.vue')},
    {path:'/roles', name:'roles',component: () => import(/* webpackChunkName: 'User_Role_Right' */ '@/components/role/Roles.vue')},
    {path:'/rights', name:'rights',component: () => import(/* webpackChunkName: 'User_Role_Right' */ '@/components/role/Rights.vue')},
    
    {path:'/categories', name:'cates',component: () => import(/* webpackChunkName: 'Cate_Param' */ '@/components/goods/Cate.vue')},
    {path:'/params', name:'params',component: () => import(/* webpackChunkName: 'Cate_Param' */ '@/components/goods/Params.vue')},

    {path:'/goods', name:'goods',component: () => import(/* webpackChunkName: 'Good_Add' */ '@/components/goods/List.vue')},
    {path:'/goods/add', name:'goods_add',component: () => import(/* webpackChunkName: 'Good_Add' */ '@/components/goods/Add.vue')},

    {path:'/orders', name:'orders',component: () => import(/* webpackChunkName: 'Order_Report' */ '@/components/order/Order.vue')},
    {path:'/reports', name:'reports',component: () => import(/* webpackChunkName: 'Order_Report' */ '@/components/report/Report.vue')},
    ]
}
]

const router = new VueRouter({
  routes
})
//路由导航守卫
router.beforeEach((to,from,next)=>{
  const tokenId = window.sessionStorage.getItem('token');//获取浏览器缓存的用户信息
  if(to.path=='/login') return next()  //放行
  if(!tokenId){
    return next('/login') //强制到login
  }
  next()
})

export default router
